package com.david.concurrent.sleep;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wade
 * @version 1.0
 * @date 2021/7/20 16:06
 */
public class SleepDemo {

    private static final Lock lock=new ReentrantLock();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("child t1 is in sleep");
                Thread.sleep(2000L);
                System.out.println("child t1 is in awaked");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        });
        Thread t2 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("child t2 is in sleep");
                Thread.sleep(2000L);
                System.out.println("child t2 is in awaked");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        });
        t1.start();
        t2.start();
    }
}
